<template>
  <div class="app-container">
    <!-- 添加或修改派车信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="选择车辆" prop="selectedVehicles">
          <el-transfer
            filterable
            filter-placeholder="请输车牌号"
            v-model="selectedVehicles"
            :data="vehicleOptions"
            :titles="['车辆列表', '派车列表']">
          </el-transfer>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan } from "@/api/transport/plan";
import { isNotNull } from "@/utils/validate";
import { listVehicleOption } from "@/api/vehicles/vehicle";

export default {
  name: "Vehicle",
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 车辆选项
      vehicleOptions: [],
      // 选择车辆
      selectedVehicles: [],
      // 表单校验
      rules: {
      }
    };
  },
  
  methods: {
    init(open, title, transportPlanNumber, transportPlanRow) {
      this.reset();
//      this.open = open;
      this.title = title;
      this.$set(this.form, 'transportPlanId', transportPlanRow.transportPlanId);
      this.$set(this.form, 'transportPlanStatus', '已派车');
      let dispatchVehicleNumber = transportPlanRow.dispatchVehicleNumber;
      if (dispatchVehicleNumber) {
        this.selectedVehicles = dispatchVehicleNumber.split(',');
      }
      this.$set(this.form, 'dispatchVehicleNumber', dispatchVehicleNumber);
      this.$set(this.form, 'remark', transportPlanRow.remark);
 
      listVehicleOption({}).then(response => {
        response.data.forEach( (item) => {
          if(item.vehicleType != '挂车') {
            this.vehicleOptions.push({
              label: item.vehicleLicenseNumber,
              key: item.vehicleLicenseNumber,
            });
          }
        });
        this.open = open;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.vehicleOptions = [];
      this.selectedVehicles = [];
      this.form = {
        transportPlanId: null,
        transportPlanStatus: null,
        dispatchVehicleNumber: null,
        remark: null
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm() {
      if(isNotNull(this.selectedVehicles)) {
        this.form.dispatchVehicleNumber = this.selectedVehicles.join(',');
      } else {
        this.form.dispatchVehicleNumber = '';
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          updatePlan(this.form).then(response => {
            this.$modal.msgSuccess("派车成功");
            this.open = false;
            this.$parent.getList();
          });
        }
      });
    },
    filterMethod(query, item) {
      return item.key.indexOf(query) > -1;
    },

  }
};
</script>
